package project.dzz.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import project.dzz.pojo.*;


import java.util.List;

/**
 *
 */
@Component
public interface MESMapper {
    /**
     * 添加任务,删除任务，加载任务，完成任务
     */
    @Insert("insert into mes_task (taskStyle,batch,productlineNum,planQuantity,releaseTime,remark,taskStatus) value " +
            "(#{taskStyle},#{batch},#{productlineNum},#{planQuantity},#{releaseTime},#{remark},#{taskStatus})")
    //返回受影响的行数值
    public int addTask(Task taskClass);

    @Insert("delete from mes_task where taskStyle=#{taskStyle} and batch=#{batch} and productlineNum=#{productlineNum}")
    public int delTask(Task taskClass);

    @Select("select taskStyle,batch,productlineNum,planQuantity,remark,taskStatus from mes_task where TO_DAYS(releaseTime) =TO_DAYS(NOW())")
    public List<TaskInfo> getTask();

    @Update("update mes_task set taskStatus=1 where taskStyle=#{arg0} and batch=#{arg1} and productlineNum=#{arg2} and taskStatus=0")
    public int comTask(String taskStyle,String batch,int productlineNum);
    /**
     * 查询所有的工序名称
     */
    @Select("select * from pro_config")
    public List<ProcessConfig> getProcessConfig();

    /**
     * 根据工序id查询参数配置表
     */
    @Select("select parameterName from para_config a inner join pro_para b on a.parameterId=b.parameterId where processId=#{processId} ")
    public List<String> getParaName(int processId);

    /**
     * 根据工序id查询b部件配置表
     */

    @Select("select componentName from comp_config a inner join pro_comp b on a.componentId=b.componentId where processId=#{processId} ")
    public List<String> getCompName(int processId);

    /**
     * 根据产品条码查询其所做的工序及其对应的参数名称、部件名称和具体的生产参数
     */
    @Select("select a.* from pro_config a inner join pdt_info b on a.processId=b.processId where b.productCode=#{productCode}")
    public List<ProcessConfig> getprocessName(String productCode);

    @Select("select a.* from comp_config a inner join pro_comp b on a.componentId=b.componentId inner join comp_info c on c.processId=b.processId " +
            "where c.productCode=#{productCode}")
    public List<CompConfig> getCompNames(String productCode);
    @Select("select batch from pdt_info where productCode=#{productCode} limit 1")
    public String getBatch(String productCode);
    @Select("select c.parameterName,b.`value`,a.qualifiedMark,a.productionTime,a.productlineNum,a.deviceNum,a.stationNum,a.employeeCode from pdt_info a " +
            "inner join para_info b on a.productCode=b.productCode and a.processId=b.processId inner join para_config c on b.parameterId=c.parameterId " +
            "where a.productCode=#{arg0} and a.processId=#{arg1}")
    public List<ChildrenSearch> getparaValue(String productCode, int processId);

    @Select("select value from comp_info where productCode=#{arg0} and componentId=#{arg1}")
    public String getcompBatch(String productCode, int componentId);

    /**
     * 根据所选条件查询不合格产品个数及条码
     */
    @Select("select productCode from pdt_info where processId=#{arg0} and productlineNum=#{arg1}"+
            " and productionTime between date(#{arg2}) and date(#{arg3}) and deviceNum=#{arg4} and batch=#{arg5} and stationNum=#{arg6} and employeeCode=#{arg7} AND qualifiedMark=0")
    public List<String> getFailedInfo(int processId,int productlineNum,String startTime,String endTime,int deviceNum,String batch,int stationNum,String employeeCode);




    /**
     * 根据选定的工序查询相应的生产信息
     * 首先根据参数名称查询参数id
     * 再根据参数id，批次号，时间区间查询生产信息
     */
    @Select("select processId from pro_config where processName=#{processName}")
    public List<Integer> getProcessId(@Param("processName") String processName);

    @Select("select batch,productionTime,count(*),sum(case when qualifiedMark=0 then 1 else 0 end) from pdt_info where processId=#{arg0} " +
            " and productionTime between date(#{arg1}) and date(#{arg2}) and batch=#{arg3} and productlineNum=#{arg4}")
    public List<ChildrenReport> getproductReport(int processId,String startTime,String endTime,String batch,int productlineNum);


    /**
     * 根据员工条码查询工序id
     */
    @Select("SELECT DISTINCT processId FROM pdt_info WHERE employeeCode=#{employeeCode}")
    public List<Integer> getEmployProcessId(String employeeCode);

    /**
     * 根据员工序id查询工序名
     */
    @Select("SELECT  processName FROM pro_config WHERE processId=#{processId}")
    public String getProNameById(int processId);


    /**
     * 根据员工条码查询员工生产情况
     */
    @Select("select count(*) from pdt_info where employeeCode=#{arg0} and processId=#{arg1} and productionTime between date(#{arg2}) and date(#{arg3})")
    public int getEmployeeInfo(String employeeCode, int processId,String startTime,String endTime);

    /**
     * Andon事件有无判断,获取安灯事件，安灯事件确认解决
     */
    @Select("select count(*) from mes_andon where andonStatus=1")
    public int getAndoncount();

    @Select("select andonStyle,productlineNum,processId,deviceNum,startTime from mes_andon where andonStatus=1")
    public List<AndonEvent> getAndonEvent();

    @Update("update mes_andon set contact=#{contact},tel=#{tel},contactContent=#{contactContent},endTime=#{endTime},remark=#{remark},andonStatus=0 where andonStyle=#{andonStyle} and startTime=#{startTime}")
    public int fininshAndon(@Param("contact") String contact, @Param("tel") String tel, @Param("contactContent") String contactContent, @Param("endTime") String endTime, @Param("remark") String remark, @Param("andonStyle") int andonStyle, @Param("startTime") String startTime);

    /**
     * 向配置表关联表中插入数据
     */
    @Select("select count(*) from pro_config where processId=#{processId}")
    public int processId(int processId);
    @Select("select count(*) from para_config where parameterId=#{parameterId}")
    public int parameterId(int parameterId);
    @Select("select count(*) from comp_config where componentId=#{componentId}")
    public int componentId(int componentId);

    @Insert("insert into pro_config (processId,processName) value (#{arg0},#{arg1})")
    public int addProcess(int processId,String processName);

    @Insert("insert into para_config (parameterId,parameterName) value (#{arg0},#{arg1})")
    public int addPara(int parameterId, String parameterName);

    @Insert("insert into para_config (parameterId,parameterName) value (#{arg0},#{arg1})")
    public int addComp(int componentId, String componentName);

    @Insert("insert into pro_para (processId,parameterId) value (#{arg0},#{arg1})")
    public int addProPara(int processId, int parameterId);

    @Insert("insert into pro_comp (processId,componentId) value (#{arg0},#{arg1})")
    public int addProComp(int processId, int componentId);

    /**
     * 查询系统配置信息
     */
    @Select("select a.parameterName,a.parameterId from para_config a inner join pro_para b on a.parameterId=b.parameterId where b.processId=#{processId}")
    public List<ParaChildren> getParachildren(int processId);

    @Select("select a.componentName,a.componentId from comp_config a inner join pro_comp b on a.componentId=b.componentId where b.processId=#{processId}")
    public List<CompChildren> getCompchildren(int processId);
    /**
     * 得到看板信息
     */
    @Select("select a.processName b.deviceNum,b.andonStatus,b.batch,b.count(*),b.sum(case when qualifiedMark=0 then 1 else 0 end) from pro_config a inner join pdt_info b on a.processId=b.processId ")
    public List<Elect> getElectInfo();
}
